import 'dart:ui';

import 'package:amway_superapp_creator_plugin/analysis/zhuge_share.dart';
import 'package:amway_superapp_creator_plugin/config/colors_config.dart';
import 'package:amway_superapp_creator_plugin/config/config_function.dart';
import 'package:amway_superapp_creator_plugin/config/enum_config.dart';
import 'package:amway_superapp_creator_plugin/utils/event_bus_utils.dart';
import 'package:amway_superapp_creator_plugin/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_flavor/flutter_flavor.dart';
import 'package:flutter_screenutil/screenutil.dart';
//import 'package:fluwx/fluwx.dart' as fluwx;


/*class CustomerShareWechat {

  static weChatShareMiniProgram(String ada, String title,
      String thumbnail) async {
    var model = new fluwx.WeChatShareMiniProgramModel(
        webPageUrl: FlavorConfig.instance.variables["webPageUrl"],
        path: FlavorConfig.instance.variables["creatorPath"] + ada,
        miniProgramType: FlavorConfig.instance.variables["miniProgramType"] == 0
            ? fluwx.WXMiniProgramType.RELEASE
            : fluwx.WXMiniProgramType.PREVIEW,
        userName: FlavorConfig.instance.variables["creatorUserName"],
        title: title,
        thumbnail: fluwx.WeChatImage.asset(
            '${thumbnail}?package=amway_superapp_creator_plugin',
            suffix: '.png'));
    fluwx.isWeChatInstalled.then((value) async =>
    value ? await fluwx.shareToWeChat(model) : ToastUtils.showShortToast(
        "未安装微信"));
  }


  static launchWeChatMiniProgram(String ada) async {
    var path = FlavorConfig.instance.variables["creatorPath"] + ada;
    fluwx.isWeChatInstalled.then((value) async {
      if(value){
        await fluwx.launchWeChatMiniProgram(
          username: FlavorConfig.instance.variables["creatorUserName"],
          path: path,
          miniProgramType: FlavorConfig.instance.variables["miniProgramType"] == 0
              ? fluwx.WXMiniProgramType.RELEASE
              : fluwx.WXMiniProgramType.PREVIEW,
        );
      }else{
        ToastUtils.showShortToast(
            "未安装微信");
      }
    });

  }
}*/


class CustomerShareView {
  final List buttonTitles = ["分享给好友", "生成分享海报", "分享链接"];
  final List buttonImages = [
    'picture/icon_shareToFriends.png',
    'picture/icon_sharePic.png',
    'picture/icon_shareLink.png'
  ];

  static showShareView({BuildContext context,
    String ada,
    String shareTitle,
    String shareThumbnail,
    String shareLink}) {
    showModalBottomSheet(
        context: context,
        backgroundColor: Color.fromRGBO(205, 249, 250, 0.2),
        elevation: 2,
        builder: (BuildContext context) {
          return Container(
            height: ScreenUtil().setHeight(267),
            color: Colors.white,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  padding: EdgeInsets.only(
                      top: ScreenUtil().setHeight(18),
                      right: ScreenUtil().setWidth(20),
                      left: ScreenUtil().setWidth(20)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(),
                      Container(
                        child: Text(
                          "分享",
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(20),
                              color: Color.fromRGBO(44, 44, 44, 1.0),
                              fontWeight: FontWeight.w600),
                        ),
                      ),
                      GestureDetector(
                        onTap: () {
                          /*CustomerShareWechat.weChatShareMiniProgram(ada,
                              shareTitle, shareThumbnail);*/

                          CustomerShareView()._dismiss(context);
                        },
                        child: Image.asset(
                          'picture/icon_dialogClose.png',
                          width: ScreenUtil().setWidth(16),
                          height: ScreenUtil().setHeight(16),
                          package: 'amway_superapp_creator_plugin',
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(bottom: ScreenUtil().setHeight(66)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,

                    children:
                    List.generate(
                        CustomerShareView().buttonTitles.length, (index) {
                      return Container(
                        child: CustomerShareView()._buttonWidget(
                          context: context,
                          ada: ada,
                          title: CustomerShareView().buttonTitles[index],
                          imageName: index == 1
                              ? 'picture/icon_share_noopen.png'
                              : CustomerShareView().buttonImages[index],
                          index: index,
                          shareTitle: shareTitle,
                          shareThumbnail: shareThumbnail,
                          shareLink: shareLink,
                        ),
                      );
                    }).toList(),
                  ),
                ),
              ],
            ),
          );
        });
  }

  //
  // imCustomerShareView().buttonImages[index],
  // index,
  // shareTitle,
  // shareThumbnail,
  // shareLink,
  _buttonWidget({BuildContext context,
    String ada,
    String title,
    String imageName,
    int index,
    String shareTitle,
    String shareThumbnail,
    String shareLink}) {
    String webPageUrl = FlavorConfig.instance.variables["webPageUrl"];
    String path = FlavorConfig.instance.variables["creatorPath"] + ada;
    int miniProgramType = FlavorConfig.instance.variables["miniProgramType"];
    String userName = FlavorConfig.instance.variables["creatorUserName"];
    String thumbnail = shareThumbnail;
    String package = "amway_superapp_creator_plugin";

    return GestureDetector(
      onTap: () {
        switch (index) {
          case 0:
            print("分享给好友 action");
            _dismiss(context);
            Map<String, dynamic> shareData = {};
            shareData['webPageUrl'] = webPageUrl;
            shareData['path'] = path;
            shareData['miniProgramType'] = miniProgramType;
            shareData['userName'] = userName;
            shareData['thumbnail'] = thumbnail;
            shareData['package'] = package;
            shareData['title'] = shareTitle;
            shareData['shareType'] = "miniProgram";
            shareData['shareAction'] = WeChatScene.SESSION;
            shareData['option'] = NotifySuperOption.SHARE;

            ConfigFunctionCallBack.optionCallback?.call(shareData);
            //eventBus.fire(NoticesEvent(shareData));
            ZhugeShareAnalysis.shareAction(true);
            //  CustomerShareWechat.weChatShareMiniProgram(ada, shareTitle, shareThumbnail);
            break;
          case 1:
            print("生成分享海报 action");
            _dismiss(context);
            break;
          case 2:
            print("分享链接 action");
            _dismiss(context);
            if (shareLink == null) {
              ToastUtils.showShortToast('分享链接获取失败,无法分享，请稍后再试');
              return;
            }
            Clipboard.setData(ClipboardData(text: shareLink));
            ToastUtils.showShortToast('链接已复制到剪贴板');
            ZhugeShareAnalysis.shareAction(false);
            break;
          default:
            break;
        }
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [

          Image.asset(
            "$imageName",
            width: ScreenUtil().setWidth(54),
            height: ScreenUtil().setHeight(54),
            package: 'amway_superapp_creator_plugin',
          ),

          Padding(
            padding: EdgeInsets.only(top: ScreenUtil().setHeight(9)),
            child: Text(title,
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                    color: index==1?ColorsConfig.c_2c2c2c.withOpacity(0.5):Color.fromRGBO(0, 0, 0, 0.3))),
          ),
        ],
      ),
    );
  }

  _dismiss(BuildContext context) {
    Navigator.pop(context);
  }
}
